#!/bin/bash


:<<EOF
    字符串使用
    拼接
    判断
    包含
    分割

EOF



year=2024
month=07
date=06


function concat(){
    dateStr=$1$2$3
}
concat  $year $month  $date
# dateStr=$year-$month-$date
echo "concat  function build ==> $dateStr"

newline=$'\n'
dateStr=$year$newline$month$newline$date
echo "append new line ==>  ${dateStr}"



#字符串 不相等
if [[   $month = $date  ]] ; then

    echo  "month ==  day "

else
    echo  "month  != day "

fi

if [[   $month == $date  ]] ; then

    echo  "month ==  day "

else
    echo  "month  != day "

fi

if [[   $month != $date  ]] ; then
    echo  "month  != day "
else
    echo  "month ==  day "
fi





#字符串 trim
dateStr="    2021-10-15    "
dateStr=$(echo $dateStr | xargs)
echo  "after trim ==>     ${dateStr} "





#字符串 length
echo  "length of  ${dateStr} is  ${#dateStr} "  #best performance
echo  "length of  ${dateStr} is  $(expr length  ${dateStr})"





#字符串  substr
echo  "${dateStr} substr(0, 4)  is  ${dateStr:0:4} "
echo  "${dateStr}  year part  is    ${dateStr:0:4} "
echo  "${dateStr}  month part   is  ${dateStr:5:2} "
echo  "${dateStr}  day  part   is   ${dateStr:8:2} "






#字符串替换  replace, 注意, 这个不是 $()  而是  ${}
dataStr=${dateStr/2024/'2025'}
echo  "after  replace ==>  $dataStr"


#字符串替换  replaceAll
dateStr="2025-02-24"
dataStr=${dateStr//'-'/''}
echo  "after  replaceAll ==>  $dataStr"


# 字符串  is  null
if [[   $dateStr1 == ''  ]] ; then
    echo  "dateStr1 is null "
else
    echo  "dateStr1 is not null "
fi


# 字符串 getDeafault
getTransDateByDefault=${dateStr:-'2025-11-11'}
echo  "getTransDateByDefault is  ${getTransDateByDefault} "
getTransDateByDefault=${dateStr1:- $(date  +%Y-%m-%d)}
echo  "getTransDateByDefault is  ${getTransDateByDefault} "





echo   " today is  $(date)"




str="runoob is a great site"
echo $(expr index "$str" is  )




# 字符串包含 contains
if [[ $dateStr  =~   $year ]]; then

    echo "字符串包含 ${dateStr}  contains  ${year} "

else
    echo "字符串不包含 ${dateStr} not  contains  ${year} "

fi

str='ads_customer_tranx_diff_cost_sum_di_9999610000_101_20240626'
substr='ads_customer_sum_di'

if [[ $str  =~   $substr ]]; then
    echo "字符串 ${str}  包含  ${substr} "
else
    echo "字符串 ${str}  不包含  ${substr} "
fi


username="sky"
if [[ $dateStr  =~   $username ]]; then
    echo 字符串包含
else
    echo 字符串不包含
fi


if [[ $((0)) ]]; then
    echo error
else
    echo success
fi


if [[ $((1)) ]]; then
    echo error
else
    echo success
fi




#字符串 split

str='_126943457805_068703.jpg'
arr=( $(echo  $str|   tr '_'  ' '  )  )
echo  ${arr[@]}


str="ONE,TWO,THREE,FOUR"
arr=($(echo $str | tr ',' ' '))
echo ${arr[@]}


for((i = 0  ;  i < ${#arr[@]}; i++))
do
echo "item is  ${#arr[$i]} "
done




start='2024-11-17'
end='2024-11-20'



# 比较字符串A和B的大小
if [[ "$start" < "$start" ]]; then
echo "字符串A小于B"
fi
if [[ "$start" > "$start" ]]; then
echo "字符串A大于B"
fi



if [[ "$start" > "$end" ]]; then
    echo  'right'
else
    echo  'error'
fi







